package com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibletask;

import android.content.Context;
import android.text.TextUtils;

import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundiblebeans.FundibleASRequestPms;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundiblehttp.FundibleASReqstManger;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundiblelogger.FundibleASLger;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibleservices.FundibleASBtryIfo;
import com.test.fundible.fundible_00_path.Paths_fundible;
import com.test.fundible.fundible_00_utils.FundibleUtil;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

public class FundibleASDevcBtyTsk implements Supplier<String> {

    private final static String TAG = "ASDeviceBatteryTask";

    private final String mTaskId;
    private final Context mContext;

    public String v6iP="";
    public String eOcR="";
    public String mN96="";
    public String r03o="";
    public String eo3B="";
    public String ei6n="";
    public String ilKU="";
    public String w5b0="";


    public FundibleASDevcBtyTsk(Context context, String taskId) {
        this.mContext = context;
        this.mTaskId = taskId;
    }

    @Override
    public String get() {
        // 日志标记
        FundibleASLger.d_fundible(TAG, TAG + " 执行中...");

        // 过滤条件
        if (TextUtils.isEmpty(Paths_fundible.PATH_BATTERY)) {
            return mTaskId + "";
        }

        setR03o("u54");

        // 创建任务
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        CountDownLatch latch = new CountDownLatch(1);
        AsyncBtyTsk_fundible asyncTask = new AsyncBtyTsk_fundible(mContext, latch, mTaskId);
        executorService.submit(asyncTask);

        setV6iP("rt8h");

        // 等待任务完成
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return mTaskId + "";
    }

    static class AsyncBtyTsk_fundible implements Runnable {
        private final CountDownLatch latch;
        private final Context mContext;
        private final String mTaskId;

        AsyncBtyTsk_fundible(Context context, CountDownLatch latch, String taskId) {
            this.mContext = context;
            this.latch = latch;
            this.mTaskId = taskId;
        }

        @Override
        public void run() {
            FundibleASBtryIfo batteryInfo = new FundibleASBtryIfo();
            batteryInfo.getBatteryInfo_fundible(mContext, batteryInfoBean -> {
                FundibleASRequestPms netParams = new FundibleASRequestPms();
                netParams.setDBG_fundible(batteryInfoBean);
//                String result = new Gson().toJson(netParams).trim();
                String result = netParams.toEncryptParams_fundible(Paths_fundible.PATH_BATTERY);

                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                    String url = FundibleUtil.getCommonPath_fundible();
                    new FundibleASReqstManger.Builder_fundible().setReqId_fundible(this.mTaskId).setUrl_fundible(url).setBody_fundible(result).build_fundible().doPostRqst_fundible();

                    return mTaskId + "";
                });

                future.thenRun(latch::countDown);
            });

        }
    }

    public void setV6iP(String v6iP) {
        this.v6iP = v6iP;
    }

    public void seteOcR(String eOcR) {
        this.eOcR = eOcR;
    }

    public void setmN96(String mN96) {
        this.mN96 = mN96;
    }

    public void setR03o(String r03o) {
        this.r03o = r03o;
    }

    public void setEo3B(String eo3B) {
        this.eo3B = eo3B;
    }

    public void setEi6n(String ei6n) {
        this.ei6n = ei6n;
    }

    public void setIlKU(String ilKU) {
        this.ilKU = ilKU;
    }

    public void setW5b0(String w5b0) {
        this.w5b0 = w5b0;
    }

    public String getmTaskId() {
        return mTaskId;
    }

    public Context getmContext() {
        return mContext;
    }

    public String getV6iP() {
        return v6iP;
    }

    public String geteOcR() {
        return eOcR;
    }

    public String getmN96() {
        return mN96;
    }

    public String getR03o() {
        return r03o;
    }

    public String getEo3B() {
        return eo3B;
    }

    public String getEi6n() {
        return ei6n;
    }

    public String getIlKU() {
        return ilKU;
    }

    public String getW5b0() {
        return w5b0;
    }
}
